package com.athuiju.fenxiaoback.controller.front;


import com.athuiju.fenxiaoback.entity.*;
import com.athuiju.fenxiaoback.entity.vo.FindSkuIdInfoVo;
import com.athuiju.fenxiaoback.entity.vo.FrontGoodVo;
import com.athuiju.fenxiaoback.entity.vo.TbGoodsQuery;
import com.athuiju.fenxiaoback.entity.vo.TbSpuQuery;
import com.athuiju.fenxiaoback.service.*;
import com.athuiju.fenxiaoback.utils.PageUtils;
import com.athuiju.fenxiaoback.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * spu表，该表描述的是一个抽象性的商品，比如 iphone8 前端控制器
 * </p>
 *
 * @author athuiju
 * @since 2021-06-30
 */

@Api( description = "商品", tags = "商品")
@RestController
@RequestMapping("/fenxiaoback/goodsfront")
public class GoodsFrontController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private TbCategoryService categoryService;

    @Autowired
    private TbBrandService brandService;

    @Autowired
    private TbSpuService spuService;

    @Autowired
    PmsSpuGenericParamValueService genericParamValueService;

    @Autowired
    TbSkuService skuService;

    @Autowired
    PmsSkuSaleParamValueService skuSaleParamValueService;


    @ApiOperation(value = "输入商品关键字搜索---分页查询")
    @PostMapping("spucondition")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = TbSpu.class),
    })
    public R pageQueryCondition(
            @ApiParam(name = "goodsQuery", value = "查询对象", required = false)
            @RequestBody TbSpuQuery goodsQuery){
        Long current = goodsQuery.getCurrent();
        Long limit = goodsQuery.getLimit();

        //创建Page对象
        Page<TbSpu> page =new Page<>(current,limit);
        //调用Service
        goodsService.pageQuery(page, goodsQuery);
        //得到当前页数
        List<TbSpu> records = page.getRecords();
        if (records==null){
            return R.error().put("msg","没有找到任何商品");
        }

        return R.ok().put("data",records);
    }



    @ApiOperation(value = "获取商品信息")
    @GetMapping("getGoodParams/{spuid}")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = FrontGoodVo.class),
    })
    public R getGoodInfo(@ApiParam("示例：1")@PathVariable String spuid){

        FrontGoodVo goodinfo=new FrontGoodVo();
        //1.查询spu信息
        TbSpu tbSpu = spuService.getById(spuid);
        if(tbSpu.getSubTitle() ==null || "".equals(tbSpu.getSubTitle())){
            tbSpu.setSubTitle("null");
        }
        if(tbSpu.getSellpoint() ==null || "".equals(tbSpu.getSellpoint())){
            tbSpu.setSellpoint("null");
        }
        //packingList
        if(tbSpu.getPackingList()==null || "".equals(tbSpu.getPackingList())){
            tbSpu.setPackingList("null");
        }
        //afterService
        if(tbSpu.getAfterService()==null || "".equals(tbSpu.getAfterService())){
            tbSpu.setAfterService("null");
        }
        BeanUtils.copyProperties(tbSpu,goodinfo);
        //2.根据spuid查询通用参数集合
        List<PmsSpuGenericParamValue> genericParamList = genericParamValueService.list(new QueryWrapper<PmsSpuGenericParamValue>().eq("spu_id", spuid));
        goodinfo.setGenericSpecList(genericParamList);

        //获取sku集合
        List<TbSku> skus = skuService.list(new QueryWrapper<TbSku>().eq("spu_id", spuid));
        goodinfo.setSkus(skus);


        List<PmsSkuSaleParamValue> allAkuSaleParamValues=new ArrayList<>();
        //4.查询sku销售属性集合
        for (int i=0;i<skus.size();i++){

            TbSku sku = skus.get(i);
            //OwnSpec
            if(sku.getOwnSpec()==null || "".equals(sku.getOwnSpec())){
                sku.setOwnSpec("null");
            }
            String skuid = sku.getId();
            List<PmsSkuSaleParamValue> skuSaleParamValues = skuSaleParamValueService.list(new QueryWrapper<PmsSkuSaleParamValue>().eq("sku_id", skuid));
            allAkuSaleParamValues.addAll(skuSaleParamValues);
        }
        goodinfo.setSpecialSpecList(allAkuSaleParamValues);


        Map<String, Set<String>> saleParams=new HashMap<>();

        for (PmsSkuSaleParamValue saleParamValue: allAkuSaleParamValues) {
            Set<String> values = saleParams.get(saleParamValue.getParamName());
            if (values==null){
                values=new HashSet<String>();
                //saleParams.put(saleParamValue.getParamName(),values);
            }
            values.add(saleParamValue.getParamValue());
            saleParams.put(saleParamValue.getParamName(),values);
        }

        goodinfo.setSkuSaleParams(saleParams);

        Map<String, Map<String,String>> findSkuIdInfoVoMap=new HashMap<>();

        for (PmsSkuSaleParamValue saleParamValue : allAkuSaleParamValues) {
            Map<String, String> nameValuMap = findSkuIdInfoVoMap.get(saleParamValue.getSkuId().toString());
            if (nameValuMap==null){
                nameValuMap= new HashMap<String,String>();
                findSkuIdInfoVoMap.put(saleParamValue.getSkuId().toString(),nameValuMap);
            }
            findSkuIdInfoVoMap.get(saleParamValue.getSkuId().toString()).put(saleParamValue.getParamName(),saleParamValue.getParamValue());
        }
        goodinfo.setFindSkuIdInfoVoMap(findSkuIdInfoVoMap);



        //5.查询
        return R.ok().put("data",goodinfo);
    }







}

